package com.android.presenter.info;

import android.content.Context;

import com.android.model.entity.res.CreateSessionResult;
import com.android.model.info.LocalSp;
import com.android.model.net.HttpObserver;
import com.android.presenter.base.BasePresenter;
import com.android.model.info.UserModel;
import com.android.presenter.iview.ICreateSessionView;
import com.android.util.log.LogUtil;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description: 创建 session，session 用于修改类的操作
 */

public class CreateSessionPresenter extends BasePresenter<ICreateSessionView> {

    private static final String TAG = CreateSessionPresenter.class.getSimpleName();
    private UserModel mUserModel;

    public CreateSessionPresenter(ICreateSessionView iCreateSessionView) {
        super(iCreateSessionView);
        mUserModel = UserModel.getInstance();
    }

    public void createSession(final Context context, String accountId, String ssoTradeToken, String accountTradeToken) {

        mUserModel.createSession(accountId, ssoTradeToken, accountTradeToken, new HttpObserver<CreateSessionResult>() {
            @Override
            public void onSuccess(CreateSessionResult createSessionResult) {
                LogUtil.e(TAG, "成功！");
                if (mIView != null) {
                    mIView.createSessionSuccess(createSessionResult);
                    LocalSp.getInstance(context).saveOrderTradeSession(createSessionResult.getValue(),"");
                }
            }

            @Override
            public void onError(int errType, String errMessage) {
                LogUtil.e(TAG, "失败码：" + errType + "》》信息：" + errMessage);
                if (mIView != null) {
                    mIView.createSessionFail(errType, errMessage);
                }
            }
        }, mIView.getLifeSubject());
    }

}
